import Vue from 'vue'
import Router from 'vue-router'
import routesConfig from './list.js'

Vue.use(Router);

let routes = []; //路由配置表

routesConfig.map(nav => {
  let route = handleRouterConfig(nav);
  routes.push(route)
});


// 定义路由实例
const router = new Router({
  linkActiveClass: 'active',
  linkExactActiveClass: 'curr',
  routes
});

/**
 * 处理路由配置项
 * @param {} conf 配置项
 */

function handleRouterConfig(conf) {
  let path = '/' + conf.path; // 读取配置项中的 path 作为路径
  let redirect = conf.redirect;
  let name = conf.path.replace(new RegExp('\\/', 'g')); //将配置项中的 component 作为 name
  let component = resolve => require(['../pages/' + conf.component],resolve);// 根据 component 指定路径 require 模块

  let route = {path, redirect, name, component}; // 生成一条配置
  if (conf.children && conf.children instanceof Array) {
    route.children = [];
    for (let child of conf.children) {
      let childRoute = handleRouterConfig(child); // 此处判断是否有嵌套路由(二级或者三级路由),有则进行添加操作
      if (typeof childRoute !== 'undefined') {
        route.children.push(childRoute)
      }
    }
  }
  return route
}

export default router
